package com.winit.openapi.tms.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.tms.spi.booking.WarehouseInPlanService;
import com.winit.tms.spi.booking.command.WarehouseInPlanQueryCommand;
import com.winit.tms.spi.booking.vo.WarehouseInPlanVo;

/**
 * 预约计划操作Controller
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * ming.chen 	1.0  		2017-9-8 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/tms/bookingPlan")
public class BookingPlanController extends BaseController {

    @Resource
    private WarehouseInPlanService warehouseInPlanService;

    /**
     * 获取可选预约计划
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryWarehouseInPlan")
    public String queryWarehouseInPlan(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        WarehouseInPlanQueryCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new WarehouseInPlanQueryCommand();
            String warehouseCode = json.getString("warehouseCode");
            BigDecimal volume = json.getBigDecimal("volume");
            String deliveryWayType = json.getString("deliveryWayType");
            String bookingType = json.getString("bookingType");
            String customerCode = json.getString("customerCode");
            if (StringUtils.isEmpty(warehouseCode)) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "warehouseCode");
            }
            if (null == volume || 0 == volume.compareTo(BigDecimal.ZERO)) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "volume");
            }
            if (StringUtils.isEmpty(deliveryWayType)) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "deliveryWayType");
            }
            if (json.containsKey("isPallet")) {
                String isPallet = json.getString("isPallet");
                if (StringUtils.isEmpty(isPallet)) {
                    throw new ApiException(ErrorCode.FIELD_NOT_NULL, "isPallet");
                }
                command.setIsPallet(isPallet);
            }
            if (json.containsKey("minPalletQty")) {
                Integer minPalletQty = json.getInteger("minPalletQty");
                if (null == minPalletQty || 0 > minPalletQty) {
                    throw new ApiException(ErrorCode.FIELD_NOT_NULL, "minPalletQty");
                }
                command.setMinPalletQty(minPalletQty);
            }

            if (StringUtils.isEmpty(bookingType)) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "bookingType");
            }

            if (StringUtils.isEmpty(bookingType)) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "bookingType");
            }

            if (StringUtils.isEmpty(customerCode)) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "customerCode");
            }
            command.setSource(requestMsg.getPlatform());
            command.setWarehouseCode(warehouseCode);
            command.setVolume(volume);
            command.setDeliveryWayType(deliveryWayType);
            command.setBookingType(bookingType);
            command.setCustomerCode(customerCode);
            command.setCtx(CommandContext.getContext());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        List<WarehouseInPlanVo> list = warehouseInPlanService.queryWarehouseInPlanAsSelect(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", list);
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    
    /**
     * 获取可选预约计划
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryPlan")
    public String queryPlan(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        WarehouseInPlanQueryCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new WarehouseInPlanQueryCommand();
            String warehouseCode = json.getString("warehouseCode");
            BigDecimal volume = json.getBigDecimal("volume");
            String deliveryWayType = json.getString("deliveryWayType");
            String bookingType = json.getString("bookingType");
            String customerCode = json.getString("customerCode");
            if (StringUtils.isEmpty(warehouseCode)) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "warehouseCode");
            }
            if (null == volume || 0 == volume.compareTo(BigDecimal.ZERO)) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "volume");
            }
            if (StringUtils.isEmpty(deliveryWayType)) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "deliveryWayType");
            }
            if (json.containsKey("isPallet")) {
                String isPallet = json.getString("isPallet");
                if (StringUtils.isEmpty(isPallet)) {
                    throw new ApiException(ErrorCode.FIELD_NOT_NULL, "isPallet");
                }
                command.setIsPallet(isPallet);
            }
            if (json.containsKey("minPalletQty")) {
                Integer minPalletQty = json.getInteger("minPalletQty");
                if (null == minPalletQty || 0 > minPalletQty) {
                    throw new ApiException(ErrorCode.FIELD_NOT_NULL, "minPalletQty");
                }
                command.setMinPalletQty(minPalletQty);
            }

            if (StringUtils.isEmpty(bookingType)) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "bookingType");
            }

            if (StringUtils.isEmpty(bookingType)) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "bookingType");
            }

            if (StringUtils.isEmpty(customerCode)) {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "customerCode");
            }
            command.setSource(requestMsg.getPlatform());
            command.setWarehouseCode(warehouseCode);
            command.setVolume(volume);
            command.setDeliveryWayType(deliveryWayType);
            command.setBookingType(bookingType);
            command.setCustomerCode(customerCode);
            command.setCtx(CommandContext.getContext());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        List<WarehouseInPlanVo> list = warehouseInPlanService.queryWarehouseInPlan(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", list);
        responseMsg.setData(data);
        return SUCCESS;
    }
}
